package com.choupin.choupin.admin.controller;

import com.choupin.choupin.admin.service.IManageLotteryService;
import com.choupin.choupin.controller.BaseController;
import com.choupin.choupin.entity.JsonResult;
import com.choupin.choupin.entity.Lottery;
import com.fasterxml.jackson.databind.BeanProperty;
import org.aopalliance.intercept.Joinpoint;
import org.apache.catalina.valves.JsonAccessLogValve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/adminlotter")
public class ManageLotteryController extends BaseController {

    @Autowired
    IManageLotteryService manageLotteryService;
//    显示抽奖池
    @CrossOrigin
    @PostMapping("show")
    public JsonResult<List<Lottery>> showLotterys(@RequestBody Map<String,Object> data){
        JsonResult<List<Lottery>> jsonResult=new JsonResult<>();
         List<Lottery> lotteries=manageLotteryService.showLotterys((Integer) data.get("currentPage"),(Integer) data.get("pageSize"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(lotteries);
        return jsonResult;
    }

//    联合搜索
    @CrossOrigin
    @PostMapping("/select")
    public JsonResult<List<Lottery>> selectLotterys(@RequestBody Map<String,Object> data ){
        JsonResult<List<Lottery>> jsonResult=new JsonResult<>();
        List<Lottery> lotteries=manageLotteryService.selectLotterys((String) data.get("id"),(String) data.get("name"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(lotteries);
        return  jsonResult;
    }
//    删除抽奖池
    @CrossOrigin
    @PostMapping("/delete")
    public JsonResult<Void> delete(@RequestBody Map<String,Object> data){
        JsonResult<Void> jsonResult=new JsonResult<>();
        manageLotteryService.deleteLottery((Integer) data.get("id"));
        jsonResult.setState(SUCCESS);
        return jsonResult;
    }

//    添加抽奖池
    @CrossOrigin
    @PostMapping("/add")
    public JsonResult<Void> add(@RequestBody Map<String,Object> data){
        JsonResult<Void> jsonResult=new JsonResult<>();
        Lottery lottery=new Lottery();
        lottery.setName((String) data.get("name"));
        lottery.setInstructions((String) data.get("instructions"));
        lottery.setPrice(BigDecimal.valueOf(Long.parseLong((String) data.get("price"))));
        manageLotteryService.add(lottery,(String) data.get("phone"));
        jsonResult.setState(SUCCESS);
        return  jsonResult;
    }

    //    更新图片
    @CrossOrigin
    @PostMapping("/updateimage")
    public JsonResult<Void> updateImage(@RequestParam("name") String name  ,@RequestParam("file") MultipartFile file,@RequestParam("phone") String  phone){
        JsonResult<Void> jsonResult=new JsonResult<>();
        manageLotteryService.updateImage(name,file,phone);
        jsonResult.setState(SUCCESS);
        return  jsonResult;
    }

    //    显示单个抽奖池信息数据
    @CrossOrigin
    @PostMapping("/updateshow")
    public JsonResult<Lottery> updateShow(@RequestBody Map<String,Object> data  ){
        JsonResult<Lottery> jsonResult=new JsonResult<>();
        Lottery lottery=manageLotteryService.updateShow((Integer) data.get("id"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(lottery);
        return  jsonResult;
    }

//  更新抽奖池
@CrossOrigin
@PostMapping("/update")
public JsonResult<String> updateLottery(@RequestBody Map<String,Object> data  ){
    JsonResult<String> jsonResult=new JsonResult<>();
    String name= manageLotteryService.updateLottery((Integer) data.get("id"),(String) data.get("name"),
                                        (String) data.get("instructions"),(String) data.get("price"),
                                        (String) data.get("phone"));
    jsonResult.setState(SUCCESS);
    jsonResult.setData(name);
    return  jsonResult;
}


}
